package com.slwish.xty.ui.activity;

import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.IntentUtils;
import com.slwish.common.utils.SpUtils;
import com.slwish.common.utils.StringUtils;
import com.slwish.xty.R;
import com.slwish.xty.entity.Doclass;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.CreditPointsContract;
import com.slwish.xty.mvp.presenter.CreditPointPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查询个人信用分
 */
public class MyCreditPointsActivity extends PresenterToolbarActivity<CreditPointsContract.Presenter> implements CreditPointsContract.View {
    @BindView(R.id.tv_credit_foot)
    SuperTextView tvCreditFoot;
    @BindView(R.id.tv_credit_point_sum)
    TextView tvPointSum;
    @BindView(R.id.tv_credit_yh)
    SuperTextView tvCreditYh;
    @BindView(R.id.tv_register)
    SuperTextView tvRegister;
    @BindView(R.id.tv_user_info)
    SuperTextView tvUserInfo;
    @BindView(R.id.tv_pj)
    SuperTextView tvPj;
    @BindView(R.id.tv_yy)
    SuperTextView tvYy;
    @BindView(R.id.tv_qxyy)
    SuperTextView tvQxyy;

    @Override
    protected int getLayoutId() {
        return R.layout.act_my_credit_points;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.my_credit_points_title);
        tvCreditFoot.getLeftTextView().getPaint().setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
        tvCreditYh.getLeftTextView().getPaint().setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
        long userId = SpUtils.getLong(mContext, Contants.UserCode, 0);
        mPresenter.queryUserCredit(mContext, userId + "");
        EventBus.getDefault().post("closeDrawer");
    }

    /**
     * 查询成功
     */
    @Override
    public void queryUserCreditSuccess(UserModel model) {
        List<Doclass> doclass = model.getDoclass();
        if (doclass != null && doclass.size() > 0) {
            int pointsSum = 0;
            for (int i = 0; i < doclass.size(); i++) {
                String point = doclass.get(i).getDoPoint();
                if (doclass.get(i).getDoClass().equals("1")) {
                    tvRegister.setRightString(!StringUtils.isEmpty(point) ? point + "分" : "去完成");
                    tvRegister.setRightTextColor(!StringUtils.isEmpty(point) ? getTextColor(R.color.gray_location_text_color) : getTextColor(R.color.colorAccent));
                } else if (doclass.get(i).getDoClass().equals("2")) {
                    tvUserInfo.setRightString(!StringUtils.isEmpty(point) ? point + "分" : "去完成");
                    tvUserInfo.setRightTextColor(!StringUtils.isEmpty(point) ? getTextColor(R.color.gray_location_text_color) : getTextColor(R.color.colorAccent));
                } else if (doclass.get(i).getDoClass().equals("3")) {
                    tvPj.setRightString(!StringUtils.isEmpty(point) ? point + "分" : "去完成");
                    tvPj.setRightTextColor(!StringUtils.isEmpty(point) ? getTextColor(R.color.gray_location_text_color) : getTextColor(R.color.colorAccent));

                } else if (doclass.get(i).getDoClass().equals("4")) {
                    tvYy.setRightString(!StringUtils.isEmpty(point) ? point + "分" : "去完成");
                    tvYy.setRightTextColor(!StringUtils.isEmpty(point) ? getTextColor(R.color.gray_location_text_color) : getTextColor(R.color.colorAccent));
                } else if (doclass.get(i).getDoClass().equals("5")) {
                    tvQxyy.setRightString(!StringUtils.isEmpty(point) ? point + "分" : "去完成");
                    tvQxyy.setRightTextColor(!StringUtils.isEmpty(point) ? getTextColor(R.color.gray_location_text_color) : getTextColor(R.color.colorAccent))
                    ;
                }
                if (!StringUtils.isEmpty(point)) {
                    pointsSum = pointsSum + Integer.valueOf(point);
                }

            }
            tvPointSum.setText(pointsSum + "");
        }

    }

    private int getTextColor(int colorResId) {
        return ContextCompat.getColor(mContext, colorResId);
    }

    @Override
    protected CreditPointsContract.Presenter initPresenter() {
        return new CreditPointPresenter(this);
    }

    @OnClick({R.id.tv_user_info, R.id.tv_pj, R.id.tv_yy})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_user_info:
                if (tvUserInfo.getRightString().equals("去完成"))
                    IntentUtils.startActivity(mContext, UserInfoActivity.class);
                break;
            case R.id.tv_pj:
                if (tvPj.getRightString().equals("去完成"))
                    IntentUtils.startActivity(mContext, CommentEditActivity.class);
                break;
            case R.id.tv_yy:
                if (tvYy.getRightString().equals("去完成"))
                    IntentUtils.startActivity(mContext, CreditTourismListActivity.class);
                break;
        }

    }
}
